/**
 * API模拟器
 * 用于模拟密码相关的API接口
 */

// 模拟用户数据
const mockUsers = {
  '13800138000': {
    id: 1,
    name: '张三',
    phone: '13800138000',
    department: '平凉政企事业部',
    password: '12345', // 初始密码
    isDefaultPassword: true
  },
  '13800138001': {
    id: 2,
    name: '李四',
    phone: '13800138001',
    department: '平凉政企事业部',
    password: 'pass123',
    isDefaultPassword: false
  }
};

/**
 * 模拟延迟
 * @param {number} ms 延迟毫秒数
 */
function delay(ms = 1000) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

/**
 * 模拟登录接口
 * @param {object} data 登录数据
 */
async function mockLogin(data) {
  await delay(800);
  
  const { phone, password } = data;
  const user = mockUsers[phone];
  
  if (!user) {
    return {
      code: 400,
      msg: '用户不存在',
      data: null
    };
  }
  
  if (user.password !== password) {
    return {
      code: 400,
      msg: '密码错误',
      data: null
    };
  }
  
  // 生成模拟token
  const token = 'mock_token_' + user.id + '_' + Date.now();
  
  return {
    code: 200,
    msg: 'success',
    data: {
      token_type: 'Bearer',
      access_token: token,
      user_info: {
        id: user.id,
        name: user.name,
        phone: user.phone,
        department: user.department
      }
    }
  };
}

/**
 * 模拟检查密码状态接口
 * @param {object} data 请求数据
 */
async function mockCheckPasswordStatus(data) {
  await delay(500);
  
  const { phone } = data;
  const user = mockUsers[phone];
  
  if (!user) {
    return {
      code: 400,
      msg: '用户不存在',
      data: null
    };
  }
  
  return {
    code: 200,
    msg: 'success',
    data: {
      isDefaultPassword: user.isDefaultPassword,
      needChangePassword: user.isDefaultPassword,
      lastPasswordChangeTime: user.isDefaultPassword ? null : '2024-01-01 10:00:00'
    }
  };
}

/**
 * 模拟修改密码接口
 * @param {object} data 修改密码数据
 */
async function mockChangePassword(data) {
  await delay(1000);
  
  const { phone, oldPassword, newPassword } = data;
  const user = mockUsers[phone];
  
  if (!user) {
    return {
      code: 400,
      msg: '用户不存在',
      data: null
    };
  }
  
  if (user.password !== oldPassword) {
    return {
      code: 400,
      msg: '当前密码错误',
      data: null
    };
  }
  
  if (oldPassword === newPassword) {
    return {
      code: 400,
      msg: '新密码不能与当前密码相同',
      data: null
    };
  }
  
  // 更新密码
  user.password = newPassword;
  user.isDefaultPassword = false;
  
  return {
    code: 200,
    msg: '密码修改成功',
    data: {
      success: true,
      message: '密码修改成功'
    }
  };
}

/**
 * 模拟用户信息接口
 * @param {object} data 请求数据
 */
async function mockUserInfo(data) {
  await delay(300);
  
  // 这里应该从token中获取用户信息，简化处理
  const phone = '13800138000'; // 假设当前用户
  const user = mockUsers[phone];
  
  if (!user) {
    return {
      code: 400,
      msg: '用户不存在',
      data: null
    };
  }
  
  return {
    code: 200,
    msg: 'success',
    data: {
      id: user.id,
      name: user.name,
      phone: user.phone,
      department: user.department
    }
  };
}

/**
 * 模拟退出登录接口
 */
async function mockLogout() {
  await delay(200);
  
  return {
    code: 200,
    msg: 'success',
    data: null
  };
}

// 导出模拟函数
module.exports = {
  mockLogin,
  mockCheckPasswordStatus,
  mockChangePassword,
  mockUserInfo,
  mockLogout,
  mockUsers
}; 